/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.storage.web;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.storage.entity.Brand;
import com.jeesite.modules.storage.entity.Member;
import com.jeesite.modules.storage.entity.VipMember;
import com.jeesite.modules.storage.service.VipMemberService;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * VIP会员Controller
 */
@Controller(value = "webvipmember")
@RequestMapping(value = "${adminPath}/storage/member/vip")
public class VipMemberController extends BaseController {

    @Autowired
    protected VipMemberService vipMemberService;

    /**
     * 查询列表
     */
    @RequestMapping(value = {"list", ""})
    public String list(VipMember member, Model model) {
        model.addAttribute("member", member);
        return "modules/storage/member/vip/list";
    }

    /**
     * 查询列表数据
     */
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page<VipMember> listData(VipMember member, HttpServletRequest request, HttpServletResponse response) {
        member.setPage(new Page<>(request, response));
        return vipMemberService.findPage(member);
    }

    /**
     * 下载导入数据模板
     */
    @RequestMapping(value = "importTemplate")
    public void importTemplate(HttpServletResponse response) {
        importTemplate(response,"VIP会员 - 大仓库.xlsx");
    }

    /*
     * 读取Excel到列表
     */
    private List<VipMember> readExcelToList(MultipartFile file){
        try {
            List<VipMember> members = new ArrayList<>();
            Workbook workbook = getWorkbook(file);
            Sheet sheet = workbook.getSheetAt(0);
            for (int rowNum = 1; rowNum <= sheet.getLastRowNum(); rowNum++) {
                Row row = sheet.getRow(rowNum);
                if (row == null  || row.getLastCellNum() < 1) {
                    continue;
                }
                String phone = getCellValue(row,0);
                if(StringUtils.isBlank(phone)){
                    continue;
                }
                VipMember member = new VipMember();
                member.setPhone(phone);
                members.add(member);
            }
            return members;
        } catch (Exception ex) {
            throw ex;
        }
    }

    /*
     * 读取Excel到列表
     */
    private String check(List<VipMember> members){
        return "";
    }

    /**
     * 导入数据
     */
    @ResponseBody
    @PostMapping(value = "importData")
    public String importData(MultipartFile file) {
        try {
            List<VipMember> members = readExcelToList(file);
            String msg = check(members);
            if(StringUtils.isNotBlank(msg)){
                return renderResult(Global.FALSE, "导入出错:" + msg);
            }
            for (VipMember member : members){
                member.setId(vipMemberService.getIdByPhone(member.getPhone()));
                vipMemberService.save(member);
            }
            return renderResult(Global.TRUE,"导入成功");
        } catch (Exception ex) {
            return renderResult(Global.FALSE, "导入出错:" + ex.getMessage());
        }
    }

    /**
     * 删除预设VIP
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(VipMember member) {
        try{
            vipMemberService.delete(member);
            return renderResult(Global.TRUE, text("删除预设VIP手机号成功！"));
        }catch (Exception ex){
            return renderResult(Global.FALSE, text(ex.getMessage()));
        }
    }
}